//
//  MerchantViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/18.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "MerchantViewController.h"
#import "PhotosViewController.h"
#import "ProductDetailsViewController.h"

#import <SwipeView/SwipeView.h>
#import "MerchantGoodsTableViewCell.h"


#import "MerchantModel.h"
#import "MediaItem.h"
#import "ADModel.h"
@interface MerchantViewController ()<UITableViewDataSource,UITableViewDelegate,SwipeViewDataSource,SwipeViewDelegate>
@property (nonatomic , weak) IBOutlet SwipeView *swipeView;
@property (nonatomic , weak) IBOutlet UITextView *textview;
@property (nonatomic , weak) IBOutlet UITableView *tableview;

@property (nonatomic , strong) NSMutableArray *rfmItems;//商家图片
@property (nonatomic , strong) NSMutableArray *goodsItems;//商家商品列表
@end

@implementation MerchantViewController

-(void)awakeFromNib
{
    _rfmItems = [NSMutableArray array];
    _goodsItems = [NSMutableArray array];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self updateMerchantInfo];
    [self statisticsAd];
}

-(void)statisticsAd{
    if (_adItem != nil) {
        [[ADModel sharedModel] adStatisticsWithBackParam:_adItem.backParam result:^(BOOL success, NSString *msg) {
            
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)updateMerchantInfo{
    typeof(self) self_ = self;
    [[MerchantModel sharedModel]searchMerchantDetailWithMerchantId:_merchantid result:^(MerchantItem *merchant, NSString *msg) {
        
        [self_.rfmItems removeAllObjects];
        [self_.goodsItems removeAllObjects];
        
        if (merchant) {
            for (MediaItem *mitem in merchant.mediaItemList) {
                RFMImageViewItem *item = [RFMImageViewItem new];
                item.isav = [mitem.MEDIA_TYPE integerValue] == 2;
                item.imagePath = mitem.IMG_URL;
                item.videoPath = mitem.MEDIA_URL;
                
                [self_.rfmItems addObject:item];
            }
            
            [self_.goodsItems addObjectsFromArray:merchant.productItemList];
            
            [self_.textview setText:merchant.MERCHANT_DESC];
        }else{
            [self_.textview setText:@""];
        }
        
        [self_.swipeView reloadData];
        [self_.tableview reloadData];
    }];
}

#pragma mark -SwipeViewDataSource
- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView
{
    return [_rfmItems count];
}

- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    RFMImageView *showImageView = (RFMImageView *)view;
    if (!showImageView) {
        showImageView = [[RFMImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_swipeView.bounds), CGRectGetHeight(_swipeView.bounds))];
        
        view = showImageView;
    }
    
    RFMImageViewItem *item = _rfmItems[index];
    [showImageView setItem:item];
    
    return view;
}

#pragma mark - SwipeViewDelegate
- (void)swipeViewCurrentItemIndexDidChange:(SwipeView *)swipeView
{
    
}

- (void)swipeView:(SwipeView *)swipeView didSelectItemAtIndex:(NSInteger)index
{
    RFMImageViewItem *item = _rfmItems[index];
    
    if (item.isav) {
        AVPlayerMagnifiedManager *manager = [AVPlayerMagnifiedManager new];
        manager.manager = self;
        manager.avdata = item.videoPath ? @{@"path":item.videoPath} : nil;
        [manager play];
    }else{
        PhotosViewController *photosViewController = [PhotosViewController new];
        [photosViewController setImageUrls:@[item.imagePath]];
        [self.navigationController pushViewController:photosViewController animated:YES];
    }
}


#pragma mark - table datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_goodsItems count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MerchantGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MerchantGoodsTableViewCell"];
    
    GoodsInfoItem *item = _goodsItems[indexPath.row];
    [cell updateDataWithItem:item];
    
    return cell;
    
}




#pragma mark - table delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([sender isKindOfClass:[MerchantGoodsTableViewCell class]]) {
        NSIndexPath *indexPath = [_tableview indexPathForCell:sender];
        
        GoodsInfoItem *item = _goodsItems[indexPath.row];
        
        ProductDetailsViewController *productDetailsViewController = [segue destinationViewController];
        productDetailsViewController.productid = item.PRODUCT_ID;
    }
}



@end
